• File: customer_maintain_data - Kopi.php
  • Full Path: C:/htdocs/REEFTintegrationLog_test/REEFTintegrationLog/x__saved/customer_maintain_data - Kopi.php
  • Date Modified: 05/09/2025 7:34 AM
  • File size: 6.23 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

//======================================================================================
//
// Function: REEFTintegrationLog - Get customers
//
// Programmer: JKJ
// Date      : 2025-05-27
//
// ChatGPT Conversaion
// https://chatgpt.com/share/680e7c97-6c40-8012-83cd-aad9c3ba8ec3
//
// Copyright Reeft A/S (c) - 2025
//======================================================================================

//======================================================================================
// General config
//======================================================================================
	include "config/config.php";

//======================================================================================
// Set language
//======================================================================================

	include "include/set_language.php";

//======================================================================================
// Get input
//======================================================================================

	if (isset($_REQUEST["customer"])) {
		$customer = $_REQUEST["customer"];
	} else {
		$customer = '';
	}

	if (isset($_REQUEST["customer_type"])) {
		$customer_type = $_REQUEST["customer_type"];
	} else {
		$customer_type = '';
	}
	
	if (isset($_REQUEST["draw"])) {
		$draw = $_REQUEST["draw"];
	} else {
		$draw = 1;
	}
	

	//===============================================================
	// Pagination fields "local"/datatables
	//===============================================================

	if (isset($_REQUEST["rpyOffSet"])) {
		$rpyOffSet = $_REQUEST["rpyOffSet"];
	} else {
		$rpyOffSet = 0;
	}

	if (isset($_REQUEST["rpyPageSize"])) {
		$rpyPageSize = $_REQUEST["rpyPageSize"];
	} else {
		$rpyPageSize = 0;
	}

	if (isset($_REQUEST["rpyOrderColumn"])) {
		$rpyOrderColumn = $_REQUEST["rpyOrderColumn"];
	} else {
		$rpyOrderColumn = '';
	}


//======================================================================================
// Get order by
//======================================================================================
	$rpyOrderColumn = str_replace("|", ' ', $rpyOrderColumn);
	$aryOrderBy = explode(',', $rpyOrderColumn);
	

//======================================================================================
// Set database
//======================================================================================

	$db_name = 'customer/REEFT_integration.sqlite3';
	
//======================================================================================
// Start me up...
//======================================================================================
	$startTime = microtime(true);

//======================================================================================
// Check if database file exists
//======================================================================================
	if (!file_exists($db_name)) {
		die(json_encode(['error' => "Database file '$db_name' does not exist."]));
	}
	
//======================================================================================
// Connect to some DB
//======================================================================================

	$DFT_SQLLITE_IP	= $db_name;

	include "include/db_connect.php";

//======================================================================================
// Get number of records
//======================================================================================

	$recordsTotal = 0;

	$sql =  "SELECT tableCount as recordsTotal FROM recordCounter WHERE tableName = 'reeft_customer'";
	include "include/db_run_sql.php";

	foreach( $data as $row )
	{
		$recordsTotal = $row["recordsTotal"];
	}

	// Make integer
	$recordsTotal = intval( $recordsTotal );

	// Format it...
	$recordsTotalFormat = number_format($recordsTotal,0,",",".");
	

//======================================================================================
// Connect to the database
//======================================================================================
	try {
		$db = new PDO('sqlite:' . $db_name);
		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	} catch (PDOException $e) {
		die(json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]));
	}

//======================================================================================
// Check if required table exists
//======================================================================================
	$tableCheck = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='reeft_customer'");

	if ($tableCheck->fetchColumn() === false) {
		die(json_encode(['error' => "Table 'reeft_customer' does not exist in database '$db_name'."]));
	}

//======================================================================================
// Fetch all customer rows
//======================================================================================
	$data = [];
	$total_found = 0;
	$total_notfound = 0;

	// Set SQL and execute statement
	$query 	= "SELECT * FROM reeft_customer LIMIT $rpyPageSize OFFSET $rpyOffSet";
	$rows 	= $db->query($query)->fetchAll(PDO::FETCH_ASSOC);


	// Loop result
	foreach ($rows as $row) {
		
		// Count active/inactive
		if ((int)$row['cust_active'] === 1) {
			$total_found++;
		} else {
			$total_notfound++;
		}

		// Add useful fields
		$row['name'] 	= $row['cust_name'];
		$row['group'] 	= (int)$row['cust_group'];

		$data[] = $row;
		
	}

	$executionTime = microtime(true) - $startTime;

	// Build response
	$response = [
		'header' => [
			'db_name' => $db_name,		
			'rpyPageSize' => $rpyPageSize,		
			'rpyOffSet' => $rpyOffSet,		
			'total_found' => $total_found,
			'total_notfound' => $total_notfound,
			'total_total' => 	$recordsTotal,
			'execution_time_sec' => number_format($executionTime, 6),
			'execution_time_ms' => number_format($executionTime * 1000, 6)
		],
		'recordsTotal' => $recordsTotal,
		'recordsFiltered' => $recordsTotal,
		'data' => $data,
		'errors' => [] // placeholder
	];

	// Output as JSON
	header('Content-Type: application/json');
	echo json_encode($response, JSON_PRETTY_PRINT);

?>